﻿namespace Controllers
{
    using System;
    using Domains;
    using HtmlAgilityPack;
    using System.IO;
    using System.Collections.Generic;

    /// <summary>
    /// Controller for compare result file with WebBrowsers files
    /// </summary>
    public static class CompareWebBrowsersWithResult
    {
        /// <summary>
        /// Load the array with accordance of position in result file to each of webbdrowsers files
        /// </summary>
        /// <param name="previusVersion">The name of the file with previus version</param>
        /// <param name="customVersion">The name of the file with custom version</param>
        /// <param name="nextVersion">The name of the file with next version</param>
        /// <param name="resultVersion">The name of the file with result version</param>
        public static void Load(string previusVersion, string customVersion, string nextVersion, string resultVersion)
        {
            //Compare Previus and Result Version
            string PreviusAndResult = AppDomain.CurrentDomain.BaseDirectory + "PreviusResult.html";
            Execution.CheckAndDelete(PreviusAndResult);
            Execution.ExecCompareScript(previusVersion, resultVersion, PreviusAndResult);
            Execution.ReadFile(PreviusAndResult);
            PreviusPosition = SetAccordance(PreviusAndResult);

            //Compare Customer and Result Version
            string CustomerAndResult = AppDomain.CurrentDomain.BaseDirectory + "CustomerResult.html";
            Execution.CheckAndDelete(CustomerAndResult);
            Execution.ExecCompareScript(customVersion, resultVersion, CustomerAndResult);
            Execution.ReadFile(CustomerAndResult);
            CustomerPosition = SetAccordance(CustomerAndResult);

            //Compare Next and Result Version
            string NextAndResult = AppDomain.CurrentDomain.BaseDirectory + "NextResult.html";
            Execution.CheckAndDelete(NextAndResult);
            Execution.ExecCompareScript(nextVersion, resultVersion, NextAndResult);
            Execution.ReadFile(NextAndResult);
            NextPosition = SetAccordance(NextAndResult);
        }

        #region Private Methods

        /// <summary>
        /// Setting accordance between Result and WebBrowsers files 
        /// </summary>
        /// <param name="CompareReportFileName">The name of the Report File (compare result and web browser files)</param>
        /// <returns>Array of accrdance (index - the number of string in result, value - the number of string in web browser</returns>
        private static int[] SetAccordance(string CompareReportFileName)
        {
            HtmlAgilityPack.HtmlDocument ComparationDocument = new HtmlAgilityPack.HtmlDocument();
            Execution.ReadFile(CompareReportFileName);
            ComparationDocument.Load(CompareReportFileName);
            int[] ResultWithWebBrowser = new int[ComparationDocument.DocumentNode.SelectNodes("/html/body/table/tr").Count + 1];

            int Count = default(int);
            foreach (HtmlNode item in ComparationDocument.DocumentNode.SelectNodes("/html/body/table/tr"))
            {
                HtmlNode[] ChildNodes = new HtmlNode[11];
                int ChildCount = default(int);
                foreach (HtmlNode ChildNode in item.ChildNodes)
                {
                    ChildNodes[ChildCount] = ChildNode.Clone();
                    ChildCount++;
                }

                if (ChildNodes[7].InnerText != "&nbsp;")
                {
                    Count++;
                    if (ChildNodes[1].InnerText != "&nbsp;")
                    {
                        ResultWithWebBrowser[Count] = Convert.ToInt32(ChildNodes[1].InnerText);
                    }
                    else
                    {
                        ResultWithWebBrowser[Count] = ResultWithWebBrowser[Count - 1];
                    }
                }
            }
            return ResultWithWebBrowser;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Correspondence position in the Result file and Previus WebBrowsers file
        /// </summary>
        public static int[] PreviusPosition { get; set; }

        /// <summary>
        /// Correspondence position in the Result file and Customer WebBrowsers file
        /// </summary>
        public static int[] CustomerPosition { get; set; }

        /// <summary>
        /// Correspondence position in the Result file and Next WebBrowsers file
        /// </summary>
        public static int[] NextPosition { get; set; }

        #endregion
    }
}
